package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.NewsPagerAdapter
import com.tigercel.xiangchengyun.entity.NewsChannelTable
import com.tigercel.xiangchengyun.ui.activities.ImportantNewsActivity
import com.tigercel.xiangchengyun.ui.activities.NewsChannelActivity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.TabLayoutUtil
import com.tigercel.xiangchengyun.utils.TitleBuilder
import kotlinx.android.synthetic.main.fragment_hot_news.*
import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.support.v4.toast

/**
 * 热点界面
 */
class HotNewsFragment : Fragment() {
    private var containerView: View? = null
    //    private var titles: List<String>? = null
    val titles = mutableListOf<String>()
    val channelids = mutableListOf<String>()
    private var newsChannels: MutableList<NewsChannelTable>? = null
    private var fragments: MutableList<Fragment>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_hot_news, container, false)
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        titlebar_tv.visibility = View.GONE
        titlebar_img.visibility = View.VISIBLE
        TitleBuilder(this!!.containerView!!)
                .setLeftImage(R.drawable.btn_nav_24h)
                .setLeftOnClickListener(View.OnClickListener {
                    startActivity(Intent(activity, ImportantNewsActivity::class.java))
                })

        getChancles()
        add_channel_iv.setOnClickListener({
            startActivity(Intent(activity, NewsChannelActivity::class.java))
        })
    }

    private fun initNewsChannels() {
//        titles = listOf("推荐", "项城", "政情", "福利", "天下", "生活", "直播", "专题", "VR全景")
//        newsChannels = ArrayList()
//        newsChannels!!.add(NewsChannelTable("推荐", "tj", "", true, 1, true))
//        newsChannels!!.add(NewsChannelTable("项城", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("政情", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("福利", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("天下", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("生活", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("直播", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("专题", "tj", "", true, 1, false))
//        newsChannels!!.add(NewsChannelTable("VR全景", "tj", "", true, 1, false))
    }

    private fun initFragments() {
        fragments = ArrayList()
        for (i in 0 until channelids!!.size) {
            var fragment: Fragment = when (channelids[i].toInt()) {
                1 -> RecommendFragment.newInstance(channelids[i].toInt())!!
                else -> NewsCommonFragment.newInstance(channelids[i].toInt())!!
            }
            (fragments as ArrayList<Fragment>).add(fragment!!)
        }
    }

    private fun getChancles() {//获取频道使用
        OkGo.get<String>(Global.baseUrl + "v1/channel/format")
                .tag(this)
                .params("type", "1")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var content: String? = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        if (status == "0") {
                            val data = JsonUtil.getList("data", content!!)
//                            titles.add(getString(R.string.recommend))
                            for (i in 0 until data.size) {
                                var chanle_name = JsonUtil.get_key_string("name", data[i])
                                var chanle_id = JsonUtil.get_key_string("channel_id", data[i])
                                titles.add(chanle_name)
                                channelids.add(chanle_id)

                            }
//                            initNewsChannels()
                            initFragments()
                            viewPager.adapter = NewsPagerAdapter(childFragmentManager, titles, fragments)
                            tabLayout.setupWithViewPager(viewPager)
                            TabLayoutUtil.dynamicSetTabLayoutMode(tabLayout)

                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }
                })
    }
}
